package com.pricecmp.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DAOUtil {
	private static DAOUtil instance = null;
	
	private DAOUtil(){}
	public static DAOUtil getInstance(){
		if(instance==null){
			instance = new DAOUtil();
		}
		return instance;
	}
	
	public Connection getConnection(){
		Connection connection = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/pricecomparison","root","root");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}
	
	public ResultSet executeQuery(String sql){
		Connection conn = getConnection();
		ResultSet rs = null;
		try {
			Statement st = conn.createStatement();
			rs = st.executeQuery(sql);
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public void executeUpdate(String sql){
		Connection conn = getConnection();
		try {
			Statement st = conn.createStatement();
			st.executeUpdate(sql);
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
